/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.event;

import org.rcgwt.client.ui.grid.internal.model.IGridModel;
import org.rcgwt.client.ui.grid.internal.model.IElementModelDelta;

import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class ModelElementChangedEvent extends
        GwtEvent<ModelElementChangedHandler> {

    private static Type<ModelElementChangedHandler> TYPE;

    private static ModelElementChangedEvent currentEvent;

    private IElementModelDelta<IGridModel> gridModelDelta;

    private ModelElementChangedEvent(
            IElementModelDelta<IGridModel> gridModelDelta) {
        this.gridModelDelta = gridModelDelta;
    }

    public static Type<ModelElementChangedHandler> getType() {
        if (TYPE == null) {
            TYPE = new Type<ModelElementChangedHandler>();
        }
        return TYPE;
    }

    @Override
    public final Type<ModelElementChangedHandler> getAssociatedType() {
        return TYPE;
    }

    public static <S extends HasModelChangedHandlers & HasHandlers> void fire(
            S source, IElementModelDelta<IGridModel> gridModelDelta) {
        if (TYPE != null) {
            ModelElementChangedEvent event = new ModelElementChangedEvent(
                    gridModelDelta);
            source.fireEvent(event);
        }
    }

    @Override
    protected void dispatch(ModelElementChangedHandler handler) {
        handler.onModelElementChange(this);
    }

    public IElementModelDelta<IGridModel> getGridModelDelta() {
        return gridModelDelta;
    }

}
